package cn.demo.service.serviceimpl;

import cn.demo.Util.Page;
import cn.demo.dao.BillDao;
import cn.demo.entity.Bill;
import cn.demo.entity.provider;
import cn.demo.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author weizhongli
 * @Date 2020/3/22
 * @Version v1.0
 * @Description
 */
@Service("billService")
public class BillServiceImpl implements BillService {

    @Autowired
     private BillDao billdao;


    public List<Bill> selectbill() {
        return billdao.selectbill();
    }

    public int deletebill(int id) {
        return billdao.deletebill(id);
    }

    public int updateBill(Bill bill) {
        return billdao.updateBill(bill);
    }

    public List<Bill> selectBillandId(int id) {
        return billdao.selectBillandId(id);
    }

    public int insertBill(Bill bill) {
        return billdao.insertBill(bill);
    }

    public List<provider> proList() {
        return billdao.proList();
    }

    public Page selectBillList(int PageIndex, int PageSize, String productName, int providerId, int isPayment) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("PageIndex", PageIndex * PageSize);
        map.put("PageSize", PageSize);
        map.put("productName", productName);
        map.put("providerId", providerId);
        map.put("isPayment", isPayment);
        Page page = new Page();
        page.setPageIndex(PageIndex);
        page.setPageSize(PageSize);
        int i = billdao.countBillList(productName, providerId, isPayment);
        page.setTotalRecords(i);
        int totalPages = page.getTotalRecords() % page.getPageSize() == 0 ? page.getTotalRecords() / page.getPageSize() : page.getTotalRecords() / page.getPageSize() + 1;
        page.setPageTotalpages(totalPages);
        List<Bill> list = billdao.selectBillList(map);
        for (Bill item : list) {
            Date creationDate = item.getCreationDate();
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
            String format = sim.format(creationDate);
            item.setCtratedate(format);
            int isPayment1 = item.getIsPayment();
            if (isPayment1 == 1) {
                item.setPaymentName("未付款");
            }
            if (isPayment1 == 2) {
                item.setPaymentName("已付款");
            }
        }
        page.setList(list);
        return page;
    }

    public int countBillList(String productName, int providerId, int isPayment) {
        return billdao.countBillList(productName, providerId, isPayment);
    }
}
